package cn.edu.scau.cmi.ema.dao;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.domain.Detectdepartment;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

@Repository("DetectdepartmentDAO")
@Transactional
public class DetectdepartmentDAOImpl extends AbstractJpaDao<Detectdepartment> implements DetectdepartmentDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Detectdepartment.class }));

	@PersistenceContext()
	private EntityManager entityManager;

	public DetectdepartmentDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Detectdepartment findDetectdepartmentById(Integer id) throws DataAccessException {

		return findDetectdepartmentById(id, -1, -1);
	}

	@Transactional
	public Detectdepartment findDetectdepartmentById(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findDetectdepartmentById", startResult, maxRows, id);
			return (Detectdepartment) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Detectdepartment> findDetectdepartmentByName(String name) throws DataAccessException {

		return findDetectdepartmentByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Detectdepartment> findDetectdepartmentByName(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findDetectdepartmentByName", startResult, maxRows, name);
		return new LinkedHashSet<Detectdepartment>(query.getResultList());
	}

	@Transactional
	public Set<Detectdepartment> findDetectdepartmentByNameContaining(String name) throws DataAccessException {

		return findDetectdepartmentByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Detectdepartment> findDetectdepartmentByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findDetectdepartmentByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Detectdepartment>(query.getResultList());
	}

	@Transactional
	public Set<Detectdepartment> findDetectdepartmentByDescription(String description) throws DataAccessException {

		return findDetectdepartmentByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Detectdepartment> findDetectdepartmentByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findDetectdepartmentByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Detectdepartment>(query.getResultList());
	}

	@Transactional
	public Set<Detectdepartment> findDetectdepartmentByDescriptionContaining(String description)
			throws DataAccessException {

		return findDetectdepartmentByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Detectdepartment> findDetectdepartmentByDescriptionContaining(String description, int startResult,
			int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findDetectdepartmentByDescriptionContaining", startResult, maxRows,
				description);
		return new LinkedHashSet<Detectdepartment>(query.getResultList());
	}

	@Transactional
	public Detectdepartment findDetectdepartmentByPrimaryKey(Integer id) throws DataAccessException {
		return findDetectdepartmentByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Detectdepartment findDetectdepartmentByPrimaryKey(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findDetectdepartmentByPrimaryKey", startResult, maxRows, id);
			return (Detectdepartment) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Detectdepartment> findDetectdepartmentsByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findDetectdepartmentsByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Detectdepartment> findAllDetectdepartments() throws DataAccessException {

		return findAllDetectdepartments(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Detectdepartment> findAllDetectdepartments(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllDetectdepartments", startResult, maxRows);
		return new LinkedHashSet<Detectdepartment>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Detectdepartment> findDetectdepartmentsByAddress(Integer addressId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllDetectdepartmentsByAddress", start, max, addressId);
		return new LinkedHashSet<Detectdepartment>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Detectdepartment> findDetectdepartmentByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Detectdepartment>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Detectdepartment> findDetectdepartmentsByDetachedCriteria(CmiPagination page)
			throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Detectdepartment.class);
		Session session = (Session) entityManager.getDelegate();
		Set<Detectdepartment> detectdepartments = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return detectdepartments;
	}

	@Transactional
	public Detectdepartment findDetectdepartmentByPhysicalPrimaryKey(String name, Integer addressId)
			throws DataAccessException {
		return findDetectdepartmentByPhysicalPrimaryKey(name, addressId, -1, -1);
	}

	@Transactional
	public Detectdepartment findDetectdepartmentByPhysicalPrimaryKey(String name, Integer addressId, int startResult,
			int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findDetectdepartmentByPhyscialPrimaryKey", startResult, maxRows, name,
					addressId);
			return (Detectdepartment) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Detectdepartment entity) {
		return true;
	}
}
